App.controller('bookingConfirmController', ['$rootScope', '$scope', '$state', '$window', 'Page',function($rootScope, $scope, $state, $window, Page) {
	if(!$scope.booking || !$scope.booking.line || !$scope.booking.line.stops 
			|| !$scope.booking.plan || !$scope.booking.plan.planId
			|| !$scope.booking.rideDates || $scope.booking.rideDates.length == 0
			|| !$scope.booking.passengers || $scope.booking.passengers.length == 0){
		return false;
	}
	
	var discountParams = {
			lineId : $scope.booking.line.lineId,
			planId : $scope.booking.plan.planId,
			dates : $scope.booking.rideDates.toString(),
			passengerCount :  $scope.booking.passengers.length
		};
	$scope.booking.discounts = [];
	$scope.booking.discount = null;
	$scope.booking.discountId = null;
	Page.ajaxPost('order/getDiscount.do',discountParams,function(resp){
		$scope.booking.discounts = resp.data; 
		$scope.booking.discount = $scope.booking.discounts[0];
	},function(resp){
		
	});
	
	$scope.submitOrder = function(){
		var order = {
			    lineId: $scope.booking.line.lineId, // 线路id
			    planId: $scope.booking.plan.planId, // 方案id
			    discountId: $scope.booking.discountId, // 折扣id
			    passengers: $scope.booking.passengers,// 乘车人信息
			    dates: $scope.booking.rideDates.toString() // 乘坐日期，以逗号隔开
		};
		Page.ajaxPostQuiet('app/order/create.do',order ,function(resp){
			var orderId = resp.data;
			Page.notice("订单提交成功!");
			$state.go('user.orderPay', {orderId : orderId});
		},function(resp){
			console.log('[sumbit order error] ! - ' + resp.resultDes);
			Page.notice(resp.resultDes);
		});
	};

	$scope.selectBookingDiscount = function(discount) {
		$scope.booking.discount = angular.copy(discount);
	};
		
	$scope.bookingPreStep = function() {
		$state.go('booking.passenger');
	};
	
}]);